package com.wanpinghui.supplier.business.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.business.view.CircleCommentListView;
import com.wanpinghui.supplier.business.view.MultiImageView;
import com.wanpinghui.supplier.utils.WTimeUtil;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.business.bean.CommentBean;
import com.wanpinghui.wphlibrary.model.business.bean.ContentImg;
import com.wanpinghui.wphlibrary.model.business.bean.DynamicBean;
import com.wanpinghui.wphlibrary.model.oauth.OAuthService;
import com.wanpinghui.wphlibrary.model.oauth.UserInfoUtil;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2018/1/23
 ***/

public class CircleListAdapter extends BaseRecyclerViewAdapter<DynamicBean, CircleListAdapter.ContentHolder> {
    private OnOperationListener operationListener;
    private List<View> views;
    private TextView textView;
    private ImageView imageView;
    public CircleListAdapter(Context context) {
        super(context);
    }

    public void setOperationListener(OnOperationListener listener) {
        operationListener = listener;
    }

    @Override
    public ContentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_business_circle, parent,false);
        return new ContentHolder(view);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public void onBindViewHolder(final ContentHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.imageViewVisibleBottom.setVisibility(View.GONE);
        holder.textViewVisibleCount.setVisibility(View.GONE);
        final DynamicBean dynamicBean = datas.get(position);
        holder.textViewName.setText(dynamicBean.getUserName());
        if (TextUtil.isNotEmpty(dynamicBean.getContent())) {
            holder.textViewContent.setVisibility(View.VISIBLE);
            holder.textViewContent.setText(dynamicBean.getContent());
        }else {
            holder.textViewContent.setVisibility(View.GONE);
        }
        if (dynamicBean.getCommentCount() == 0 && dynamicBean.getThumbUpCount() == 0) {
            holder.linearLayoutCommentArea.setVisibility(View.GONE);
        }else {
            holder.linearLayoutCommentArea.setVisibility(View.VISIBLE);
        }
        if (dynamicBean.getCommentCount() == 0 || dynamicBean.getThumbUpCount() == 0) {
            holder.viewLine.setVisibility(View.GONE);
        }else {
            holder.viewLine.setVisibility(View.VISIBLE);
        }
        holder.textViewUpUser.setText(dynamicBean.getThumbUpStr());
        if (dynamicBean.getCommentCount() > 0) {
            holder.listViewComment.setDatas(dynamicBean);
        }else {
            holder.listViewComment.setDatas(null);
        }
        if (dynamicBean.getUser_id() == WPHSdk.getService(OAuthService.class).getUserId()) {
            holder.textViewDelete.setVisibility(View.VISIBLE);
        }else {
            holder.textViewDelete.setVisibility(View.GONE);
        }
        if (dynamicBean.getThumbUpCount() > 0) {
            holder.layoutLikeArea.setVisibility(View.VISIBLE);
        }else {
            holder.layoutLikeArea.setVisibility(View.GONE);
        }
        if(dynamicBean.getVisible_type() == 1 || dynamicBean.getVisible_type() == 11) {
            holder.imageViewLookPeople.setVisibility(View.GONE);
        }else {
            if(WPHSdk.getService(OAuthService.class).getUserId()==dynamicBean.getUser_id())
                holder.imageViewLookPeople.setVisibility(View.VISIBLE);
            else
                holder.imageViewLookPeople.setVisibility(View.GONE);
        }
        holder.textViewDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (operationListener != null) {
                    operationListener.onDeleteDynamic(dynamicBean);
                }
            }
        });
        holder.itemView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (textView != null && textView.isShown()) {
                        textView.setVisibility(View.GONE);
                        imageView.setVisibility(View.GONE);
                        textView = null;
                        imageView = null;
                    }
                }
                return false;
            }
        });
        holder.imageViewLookPeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.textViewVisibleCount.isShown()) {
                    holder.textViewVisibleCount.setVisibility(View.GONE);
                    holder.imageViewVisibleBottom.setVisibility(View.GONE);
                    textView = null;
                    imageView = null;
                }else {
                    if (textView != null && textView.isShown()) {
                        textView.setVisibility(View.GONE);
                        imageView.setVisibility(View.GONE);
                    }
                    textView = holder.textViewVisibleCount;
                    imageView = holder.imageViewVisibleBottom;
                    holder.textViewVisibleCount.setVisibility(View.VISIBLE);
                    holder.imageViewVisibleBottom.setVisibility(View.VISIBLE);
                    if (dynamicBean.getVisible_type() == 21) {
                        holder.textViewVisibleCount.setText("全国工程商可见，共"+dynamicBean.getEngineer_quantity()+"人");
                    }else {
                        if (TextUtil.isNotEmpty(dynamicBean.getArea_names())) {
                            String[] strings = dynamicBean.getArea_names().split(",");
                            String contentStr = dynamicBean.getArea_names();
                            if (strings.length > 2) {
                                contentStr = strings[0]+","+strings[1]+","+strings[2]+"...";
                            }
                            holder.textViewVisibleCount.setText(contentStr+"工程商可见，共"+dynamicBean.getEngineer_quantity()+"人");
                        }else {
                            holder.textViewVisibleCount.setText("部分地区工程商可见，共"+dynamicBean.getEngineer_quantity()+"人");
                        }
                    }
                }
            }
        });
        Glide.with(context).load(dynamicBean.getAvatar()).asBitmap().error(R.mipmap.my_icon_head_default).into(holder.imageViewAvatar);
        if (dynamicBean.getContentImgs() != null && dynamicBean.getContentImgs().size() > 0) {
            holder.imageViewContent.setDatas(dynamicBean.getContentImgs());
            holder.imageViewContent.setVisibility(View.VISIBLE);
        }else {
            holder.imageViewContent.setDatas(null);
            holder.imageViewContent.setVisibility(View.GONE);
        }
        if (dynamicBean.getType() == DynamicBean.DynType.TYPE_ADVEN) {
            holder.textViewRightTag.setVisibility(View.VISIBLE);
        }else {
            holder.textViewRightTag.setVisibility(View.GONE);
        }
        if (dynamicBean.getCreated_at() > 0) {
            holder.textViewTime.setText(WTimeUtil.getTimeShowStr(dynamicBean.getCreated_at()));
        }else {
            holder.textViewTime.setText("1970-01-01 08:00:00");
        }

        holder.textViewName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (operationListener != null) {
                    operationListener.onUsreDynamic(dynamicBean.getUser_id(), dynamicBean.getUserName());
                }
            }
        });

        holder.imageViewAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (operationListener != null) {
                    operationListener.onUsreDynamic(dynamicBean.getUser_id(), dynamicBean.getUserName());
                }
            }
        });

        holder.imageViewComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (operationListener != null) {
                    operationListener.onCommentClick(dynamicBean, holder.imageViewComment,dynamicBean.getUser_id(), dynamicBean.getUserName(),position);
                }
            }
        });
        holder.listViewComment.setListener(new CircleCommentListView.CommonListener() {
            @Override
            public void onCommont(CommentBean bean, int toUserId, String textStr) {
                if (operationListener != null) {
                    operationListener.onCommentPerson(dynamicBean, bean, toUserId, textStr, position);
                }
            }

            @Override
            public void onUser(int userId, String userName) {
                if (operationListener != null) {
                    operationListener.onUsreDynamic(userId, userName);
                }
            }
        });
    }

    public View getContentView(int index) {
        if (views != null && views.size() > index) {
            return views.get(index);
        }else {
            return null;
        }
    }

    public void setViews(List<View> views) {
        this.views = views;
    }

    public void changeData(DynamicBean mCurrentDynamic) {
        if (mCurrentDynamic != null && getItemCount() > 0) {
            for (int i = 0; i<datas.size();i++) {
                if (datas.get(i).getId() == mCurrentDynamic.getId()) {
                    notifyItemChanged(i);
                }
            }
        }
    }

    public void deleteDynamic(DynamicBean dynamicBean) {
        if (getItemCount() > 0) {
            for (int i = 0 ;i<datas.size();i++) {
                if (dynamicBean.getId() == datas.get(i).getId()) {
                    datas.remove(i);
                    break;
                }
            }
            notifyDataSetChanged();
        }
    }

    class ContentHolder extends RecyclerView.ViewHolder {
        ImageView imageViewAvatar;
        TextView textViewName;
        TextView textViewRightTag;
        TextView textViewContent;
        MultiImageView imageViewContent;
        TextView textViewTime;
        ImageView imageViewLookPeople;
        ImageView imageViewComment;
        TextView textViewUpUser;
        CircleCommentListView listViewComment;
        LinearLayout linearLayoutCommentArea;
        View viewLine;
        LinearLayout layoutContent;
        LinearLayout layoutLikeArea;
        TextView textViewVisibleCount;
        ImageView imageViewVisibleBottom;
        TextView textViewDelete;
        public ContentHolder(View itemView) {
            super(itemView);
            imageViewAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            textViewName = (TextView) itemView.findViewById(R.id.tv_user_name);
            textViewRightTag = (TextView) itemView.findViewById(R.id.tv_right_tag);
            textViewContent = (TextView) itemView.findViewById(R.id.tv_content);
            imageViewContent = (MultiImageView) itemView.findViewById(R.id.iv_image_content);
            textViewTime = (TextView) itemView.findViewById(R.id.tv_time);
            imageViewLookPeople = (ImageView) itemView.findViewById(R.id.iv_look_user);
            imageViewComment = (ImageView) itemView.findViewById(R.id.iv_comment);
            textViewUpUser = (TextView) itemView.findViewById(R.id.tv_thumb_up);
            listViewComment = (CircleCommentListView) itemView.findViewById(R.id.lv_comments);
            viewLine = itemView.findViewById(R.id.v_line);
            layoutLikeArea = (LinearLayout) itemView.findViewById(R.id.ll_likes_area);
            linearLayoutCommentArea = (LinearLayout) itemView.findViewById(R.id.ll_likes_comments_area);
            layoutContent = (LinearLayout) itemView.findViewById(R.id.ll_content);
            textViewVisibleCount = (TextView) itemView.findViewById(R.id.tv_visible_count);
            textViewDelete = (TextView) itemView.findViewById(R.id.tv_delete);
            imageViewVisibleBottom = (ImageView) itemView.findViewById(R.id.iv_visible_count_bottom);
            imageViewContent.setViewWidth(DensityUtil.getScreenWidth()-DensityUtil.dip2px(87));
            imageViewContent.setCircleAdapter(CircleListAdapter.this);
            itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }

    public interface OnOperationListener{
        void onCommentClick(DynamicBean dynamicBean, View parentView,int toUserId, String toUserName, int position);
        void onCommentPerson(DynamicBean dynamicBean, CommentBean bean,int toUserId, String toUserName, int position);
        void onUsreDynamic(int userId, String userName);
        void onDeleteDynamic(DynamicBean dynamicBean);
    }
}
